using AnalyStock.DataManage;
using AnalyStock.GrapImage;
using System.Timers;

namespace AnalyStock.WPFPage;
public partial class RealTimePage : Page
{

    private System.Timers.Timer timerPriceChg;
    public RealTimePage()
    {
        InitializeComponent();
        CBoxSeleConception.SelectionChanged += SeleConception_Changed;
        DViewRealTime.SelectionChanged += SeleKeyStock_Changed;
        ChartKline.SeleStocksFilled += (s, e) =>
        {
            FillConceptions();
            IniTimer();
            StartTimer();
        };
    }

    private static string conceptionName; //保留概念名称
    private void SeleConception_Changed(object sender, EventArgs e)
    {
        conceptionName = (string)CBoxSeleConception.SelectedValue;
        TextPriceTable_Tick(this, null);
    }

    private void FillConceptions()
    {
        CBoxSeleConception.ItemsSource = Conceptions.Select(n => n.Name);
        CBoxSeleConception.SelectedIndex = 0;
    }



    private void SeleKeyStock_Changed(object sender, EventArgs e)
    {
        if (DViewRealTime.CurrentItem is DailyOfTencent currentStock)
        {
            ChartKline.ChangeCurrentStock(currentStock.Stockcode);
            return;
        }
    }

    private void TextPriceTable_Tick(object sender, ElapsedEventArgs e)
    {
        TextPriceTableAsync().WithCapturaException();
    }

    private async Task TextPriceTableAsync()
    {
        var _currentPrices = await GetCurrentPriceAsync();
        //返回UI线程显示价格表
        await DViewRealTime.Dispatcher.InvokeAsync(() =>
        {
            DViewRealTime.ItemsSource = default;
            DViewRealTime.ItemsSource = _currentPrices;
        });
    }

    private static async Task<IList<DailyOfTencent>> GetCurrentPriceAsync()
    {
        var _items = new List<DailyOfTencent>();
        foreach (var s in SelectStocks.Where(n => n.Conception == conceptionName))
        {
            var _current = await FillCurrentDailyAsync(s.Ts_code);
            if (_current is not null)
            {
                _current.Stockcode = s.Ts_code;
                _items.Add(_current);
            }
        }
        return _items;
    }

    private static async Task<DailyOfTencent> FillCurrentDailyAsync(string stockCode)
    {
        return await DownLoadExtensions_Tencent.GetCurrentDailyOnTencentAsync(stockCode);
    }

    private void IniTimer()
    {
        timerPriceChg = new System.Timers.Timer
        {
            Interval = 30000,//30秒刷新一次
            AutoReset = true,
        };
        timerPriceChg.Elapsed += TextPriceTable_Tick;
        //分钟面板数据同步更新
        timerPriceChg.Elapsed += ChartKline.PaintMintuePanel_Tick;
        Loaded += (s, e) =>
        {
            StartTimer();
        };
        Unloaded += (s, e) =>
        {
            StopTimer();
        };
    }


    private void StartTimer()
    {
        if (timerPriceChg is null) { IniTimer(); }
        // timerPriceChg.Enabled = true;
        timerPriceChg.Start();
        //TextPriceTable_Tick(this, null);
    }

    private void StopTimer()
    {
        if (timerPriceChg is not null)
        {
            timerPriceChg.Stop();
            timerPriceChg.Close();
        }

    }

}

public class Pctchg_BrushConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        return (float)value switch
        {
            <= 0 => new SolidColorBrush("#649865".ConvertoColor()),
            > 0 and < 5.0f => new SolidColorBrush("#FFAE95".ConvertoColor()),
            >= 5.0f and < 9.0f => new SolidColorBrush("#F65A53".ConvertoColor()),
            >= 9.0f => new SolidColorBrush("#F22814".ConvertoColor()),
            _ => new SolidColorBrush(Colors.Transparent)
        };
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class TurnOver_BrushConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (float)value switch
        {
            > 5.0f and <= 10.0f => new SolidColorBrush("#FFBEA5".ConvertoColor()),//FFB6C1
            > 10.0f and <= 15.0f => new SolidColorBrush("#FC7c74".ConvertoColor()),
            > 15.0f and <= 25.0f => new SolidColorBrush("#F65A53".ConvertoColor()),
            > 25.0f => new SolidColorBrush("#C81c2A".ConvertoColor()),
            _ => new SolidColorBrush(Colors.Transparent)
        };
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}